package com.compass.doctor.ui.patient

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.ActivityPatientDetailsBinding
import com.compass.doctor.dialog.AppendTagDialog
import com.compass.doctor.ui.patient.fragment.PatientExamineFragment
import com.compass.doctor.ui.patient.fragment.PatientHospitalizedLogFragment
import com.compass.doctor.ui.patient.fragment.PatientPrescriptionRecordFragment
import com.compass.doctor.ui.patient.fragment.RecordInformationFragment
import com.compass.doctor.ui.patient.view.PatientBasicInformationView
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.KEY_RX_TYPE
import com.compass.framework.constant.REFRESH_PATIENT
import com.compass.framework.model.PatientInfo
import com.compass.framework.model.Tag
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.StatusBarSettingHelper

/**
 * Description： 患者资料（患者详情）
 * Date:2023/12/13 17:49
 */
class PatientDetailsActivity : BaseVBActivity<ActivityPatientDetailsBinding>() {

    companion object {
        fun start(context: Context, patientId: String?) {
            val intent = Intent(context, PatientDetailsActivity::class.java)
            intent.putExtra(KEY_PATIENT_ID, patientId)
            context.startActivity(intent)
        }
    }

    private var patientTags: ArrayList<Tag> = ArrayList()
    private lateinit var patientBasicInfoView: PatientBasicInformationView
    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()
    private val patientViewModel: PatientViewModel by viewModels()
    private var patientInfo: PatientInfo? = null
    private var patientId: String? = null
    private lateinit var seeDoctorRecordFragment: RecordInformationFragment//就诊记录
    private lateinit var prescriptionRecordFragment: PatientPrescriptionRecordFragment//处方记录
    private lateinit var inspectionRecordFragment: PatientExamineFragment//检查记录
    private lateinit var hospitalizedRecordFragment: PatientHospitalizedLogFragment//住院记录
    private lateinit var appendTagDialog: AppendTagDialog.Builder

    //添加标签回调
    private var onCustomButtonClick: OnCustomButtonClick = object : OnCustomButtonClick {
        override fun onButtonCallBack(position: Int, msgType: String?) {
            appendTagDialog= AppendTagDialog.Builder(this@PatientDetailsActivity, patientId)
            appendTagDialog.setTagsData(patientTags,false)
            appendTagDialog.show()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@PatientDetailsActivity, true)
        patientId = intent.getStringExtra(KEY_PATIENT_ID)
        initFragment()

        patientBasicInfoView = PatientBasicInformationView(this)
        patientBasicInfoView.setCallBack(onCustomButtonClick)
        mBinding.flyContainer.addView(patientBasicInfoView)
        mBinding.dslTab.tabIndicator.indicatorWidth = -2
        mBinding.dslTab.tabIndicator.indicatorEnableFlow = true
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                val toIndex = selectIndexList.first()
                mBinding.viewpager2.setCurrentItem(toIndex, false)
            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
        mBinding.viewpager2.setCurrentItem(0, false)
    }

    private fun initFragment() {

        seeDoctorRecordFragment = RecordInformationFragment()
        val seeDoctor = Bundle()
        seeDoctor.putString(KEY_PATIENT_ID, patientId)
        seeDoctorRecordFragment.arguments = seeDoctor
        fragments.add(seeDoctorRecordFragment)


        prescriptionRecordFragment = PatientPrescriptionRecordFragment()
        val prescription = Bundle()
        prescription.putString(KEY_PATIENT_ID, patientId)
        prescription.putInt(KEY_RX_TYPE, 1)
        prescriptionRecordFragment.arguments = prescription
        fragments.add(prescriptionRecordFragment)

        inspectionRecordFragment = PatientExamineFragment()
        val inspection = Bundle()
        inspection.putString(KEY_PATIENT_ID, patientId)
        inspectionRecordFragment.arguments = inspection
        fragments.add(inspectionRecordFragment)

        hospitalizedRecordFragment = PatientHospitalizedLogFragment()
        val hospitalized = Bundle()
        hospitalized.putString(KEY_PATIENT_ID, patientId)
        hospitalizedRecordFragment.arguments = hospitalized
        fragments.add(hospitalizedRecordFragment)

    }

    override fun initData() {
        showLoading()
        patientViewModel.getPatientDetails(patientId)
        patientViewModel.patientDetailsLiveData.observe(this) {
            dismissLoading()
            it?.let {info->
                patientInfo = info
                info.tags?.let { tag->
                    patientTags =tag
                    patientBasicInfoView.setPatientTags(patientTags)
                }

                patientBasicInfoView.setPatientInfo(it)
            }
        }

        patientViewModel.addLiveData.observe(this) {
            it?.let {
                patientTags.add(it)
                patientBasicInfoView.setPatientTags(patientTags)
                appendTagDialog.setTagsData(patientTags,true)
                showToast("添加成功")
                LiveDataBus.get().with(REFRESH_PATIENT).postValue(REFRESH_PATIENT)
            }

        }

        patientViewModel.deleteLiveData.observe(this) {
            var needDel: ArrayList<Tag> = ArrayList()
            for (tag in patientTags) {
                if (tag.id == it) needDel.add(tag)
            }
            patientTags.removeAll(needDel)
            showToast("删除成功")
            patientBasicInfoView.setPatientTags(patientTags)
            appendTagDialog.setTagsData(patientTags,false)

            LiveDataBus.get().with(REFRESH_PATIENT).postValue(REFRESH_PATIENT)
        }
    }

}